package com.cgbl.cg.ui.collection

import android.os.Bundle
import com.alibaba.android.arouter.facade.annotation.Route
import com.cgbl.cg.BR
import com.cgbl.cg.R
import com.cgbl.cg.adapter.SelectCollectionAdapter
import com.cgbl.cg.databinding.AppActivitySelectCollectionBinding
import com.cgbl.cg.vm.collection.SelectCollectionViewModel
import com.robot.base.base.activity.GLBaseActivity
import com.robot.base.configs.AppConstants
import com.robot.base.model.CollectDetailBean

@Route(path = AppConstants.Router.COLLECTION.A_SELECT_COLLECTION)
class SelectCollectionActivity :
    GLBaseActivity<AppActivitySelectCollectionBinding, SelectCollectionViewModel>() {
    private lateinit var selectCollectionAdapter: SelectCollectionAdapter
    override fun initContentView(savedInstanceState: Bundle?): Int =
        R.layout.app_activity_select_collection

    override fun initVariableId(): Int = BR.viewModel

    override fun initComponents() {
        addSingleTitleBar(getString(R.string.app_en_tips_95))
        initAdapter()
    }

    override fun initData() {
        super.initData()
        viewModel.refresh()
    }

    private fun initAdapter() {
        selectCollectionAdapter = SelectCollectionAdapter()
        binding.mRv.apply {
            adapter = selectCollectionAdapter
        }
    }

    override fun initViewObservable() {
        super.initViewObservable()
        binding.smartRefreshLayout.setOnRefreshListener {
            viewModel.refresh()
        }

        binding.smartRefreshLayout.setOnLoadMoreListener {
            viewModel.next()
        }

        viewModel.uc.refreshEvent.observe(this){
            it ?: return@observe
            binding.smartRefreshLayout.finishRefresh()
            binding.smartRefreshLayout.finishLoadMore()
            binding.smartRefreshLayout.setEnableLoadMore(it != 1)
        }

        viewModel.uc.refreshDataEvent.observe(this){
            if (viewModel.pageNo == 1) {
                selectCollectionAdapter.setNewInstance(it as MutableList<CollectDetailBean>?)
            }else{
                selectCollectionAdapter.addData(it)
            }
        }
    }


}